package com.udaos.modules.resource.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.adaptor.WhaleAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.resource.Ud_wx_resource;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.resource.services.UdWxResourceService;
import com.udaos.modules.section.services.UdSectionResourceService;
@IocBean
@At("/platform/resource")
@Filters({ @By(type = PrivateFilter.class) })
public class UdWxresourceController {
	
	@Inject
	private UdWxResourceService resourceService;
	@Inject
	private UdSectionResourceService  sectionResourceService;
	
	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/resource/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	
	/**
	 * 加载数据
	 * @param name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("name") String name,@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", "0");
		Subject subject = SecurityUtils.getSubject();
        Sys_user user = (Sys_user) subject.getPrincipal();
        cnd.and("company_id", "=", user.getUnit().getCompany_id());
        if (!Strings.isBlank(name))
			cnd.and("title", "like", "%" + name + "%");
		return resourceService.data(length, start, draw, order, columns, cnd, null);
	}
	/**
	 * 新增
	 * @return
	 */
	@At
	@Ok("beetl:/platform/resource/add.html")
	@RequiresAuthentication
	public void add() {
		/*Map<String,Object> map=new HashMap<String,Object>();
		//获取片区
		List<Ud_region> regions=resourceService.getRegions();
		map.put("regions", regions);
		return map;*/
	}
	/**
	 * do新增
	 * @param resource
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	public Object addDo(@Param("..") Ud_wx_resource resource,HttpServletRequest req) {
		try {
			Subject subject = SecurityUtils.getSubject();
			Sys_user user = (Sys_user) subject.getPrincipal();
			resource.setCompany_id(user.getUnit().getCompany_id());
			resource.setCreateBy(user.getId());
			resource.setCreateAt((int)(System.currentTimeMillis()/1000));
			resourceService.insert(resource);
			return Result.success("保存成功");
		} catch (Exception e) {
			System.err.println(e.getMessage());
			return Result.error("保存失败");
		}
	}
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/resource/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		/*//获取片区
		List<Ud_region> regions=resourceService.getRegions();
		map.put("regions", regions);*/
		map.put("resource", resourceService.fetch(id));
		return map;
	}
	
	/**
	 * do修改
	 * @param resource
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	public Object editDo(@Param("..") Ud_wx_resource resource,HttpServletRequest req) {
		try {
			resourceService.updateIgnoreNull(resource);
			return Result.success("修改成功");
		} catch (Exception e) {
			return Result.error("修改失败");
		}
	}
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_wx_resource resource = resourceService.fetch(id);
        	resource.setDelFlag(true);
        	resourceService.updateIgnoreNull(resource);
        	sectionResourceService.deleteByResourceId(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
	

}
